//
//  MainAccountInputCell.swift
//  d11
//
//  Created by peale on 2018/9/17.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias AccountChangeBlock = (_ money: String) -> Void
class MainAccountInputCell: UITableViewCell,UITextFieldDelegate {
    var bgImgView : UIImageView?
    var iconImgView : UIImageView?
    var titleLabel : UILabel?
    var moneyField : UITextField?
    var accountChangeBlock: AccountChangeBlock?
    func callChangeBlock(_ block: @escaping AccountChangeBlock) {
        
        accountChangeBlock = block
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.backgroundColor  = UIColor.clear
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.bgImgView = UIImageView.init()
        self.bgImgView?.image = imageNamed("icon_my_cell_bottom")
        self.contentView.addSubview(bgImgView!);
        self.bgImgView?.isUserInteractionEnabled = true
        self.bgImgView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-10)
            make.height.equalTo(51)
            make.centerX.equalTo(self.contentView)
        };
        
        self.iconImgView = UIImageView.init()
        self.iconImgView?.image = imageNamed("icon_mainAccount_lalance")
        self.bgImgView?.addSubview(self.iconImgView!)
        self.iconImgView?.snp.makeConstraints{(make) in
            make.left.equalToSuperview().offset(27)
            make.width.equalTo(15)
            make.height.equalTo(15.5)
            make.centerY.equalTo(self.bgImgView!)
        }
        
        
        self.titleLabel = UILabel.init()
        self.titleLabel?.text = "金额"
        self.titleLabel?.textColor = HexRGBAlpha(0x333333, 1)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        self.titleLabel?.textAlignment = NSTextAlignment.left
        self.bgImgView?.addSubview(self.titleLabel!)
        
        self.titleLabel?.snp.makeConstraints{(make) in
            make.left.equalTo((self.iconImgView?.snp.right)!).offset(17)
            make.width.equalTo(40)
            make.height.equalTo(25)
            make.centerY.equalToSuperview()
        }
        
        
        self.moneyField = UITextField.init()
        self.moneyField?.textColor =  HexRGBAlpha(0x8E8F90, 1)
        self.moneyField?.font = UIFont.systemFont(ofSize: 15)
        self.moneyField?.textAlignment = NSTextAlignment.right
        self.moneyField?.keyboardType  = .decimalPad
        self.moneyField?.placeholder = "请输入转帐金额"
        self.moneyField?.delegate = self
        self.bgImgView?.addSubview(self.moneyField!)
        self.moneyField?.addTarget(self, action: #selector(self.actionToChanged(_:)), for: UIControl.Event.editingChanged)
        self.moneyField?.snp.makeConstraints{(make) in
            make.right.equalTo(-33)
            make.width.equalTo(kScreenWidth-21-12-55-45-10)
            make.centerY.equalTo(self.bgImgView!)
            make.height.equalTo(51)
        }
    }
    
    func setData(money:String?) -> Void {
        moneyField?.text = money
    }
    @objc func actionToChanged(_ sender: UITextField) -> Void {

        if accountChangeBlock != nil {
            accountChangeBlock!(sender.text!)
        }
    }
//
//    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//
//        return true
//    }
}
